
package com.kanjian.music.activity;

import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kanjian.music.R;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.User;
import com.kanjian.music.network.HttpLoader;

public class LoginActivity extends BaseActivity implements LoaderCallbacks<String> {

    private EditText mEdit_userName;

    private EditText mEdit_password;

    private Button mBtn_login;

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_login);
        super.onCreate(arg0);
        initView();
    }

    private void initView() {
        mEdit_userName = (EditText) findViewById(R.id.login_username);
        mEdit_password = (EditText) findViewById(R.id.login_password);
        mBtn_login = (Button) findViewById(R.id.login_btn);
        mBtn_login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM, ApiRequestManager
                        .getLoginRequest(mEdit_userName.getText().toString(), mEdit_password
                                .getText().toString()));
                getSupportLoaderManager().initLoader(0, b, LoginActivity.this);
            }
        });
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(this, args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            if (User.localLogin(data)) {
                Toast.makeText(this, "success", 1).show();
                setResult(0);
                finish();
            }
        }
        getSupportLoaderManager().destroyLoader(0);
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
        // TODO Auto-generated method stub

    }
}
